package it.unipi.di.rasip.gui.tabs;


import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class TabMenu extends CustomItem {
	int background = 0xffffff;
	
	int cornerRadius = 6;
	int padding = 2;
	int margin = 2;
	Font font = Font.getDefaultFont();
	
	int scrollStep = 40;
	
	int selectedTab = 0;	//selected tab index
	int menuWidth = 0;		//total menu width
	int tabHeight;
	
	int viewportWidth = 0;	//visible viewport width
	int viewportX = 0;		//current viewport X coordinate
	int menuY = 3;
	
	Vector tabs;
	
	public TabMenu(int width) {
		super(null);
		//this.tabs = tabs;
		this.tabs = new Vector();
		
		this.viewportWidth = width;
		
		//initialize();
	}
	
	public void addTab(Tab t) {
		if(tabs.isEmpty()) {
			t.setX(0);
		} else {
			Tab lt = (Tab)tabs.lastElement();
			t.setX(lt.getX()+t.getWidth()+margin);
		}
		tabs.addElement(t);
		this.invalidate();
	}
	
	public void initialize() {
		//tabHeight = font.getHeight() + cornerRadius + 2 * padding;
		//tabHeight = (int)((Tab)tabs.elementAt(0)).getHeight();
		tabHeight = 25;
		
		menuWidth = 0;
		boolean first = true;
		for (Enumeration e = tabs.elements(); e.hasMoreElements();) {
			menuWidth += ((Tab)e.nextElement()).getWidth();
			if(first) {
				first = false;
			} else {
				menuWidth += margin;
			}
		}
	}
	
	public void goRight() {
		go(+1);
	}
	public void goLeft() {
		go(-1);
	}
	public void goLast() {
		this.go(tabs.size());
	}
	
	private void go(int delta) {
		int min = Math.min(tabs.size() - 1, selectedTab + delta);
		int newTab = Math.max(0, min);
		boolean scroll = true;
		
		Tab t = (Tab)tabs.elementAt(newTab);
		if(newTab != selectedTab)// && isTabVisible(newTab))
		{
			selectedTab = newTab;
			if(	(delta > 0 && t.getX() + t.getWidth() > viewportX + viewportWidth) || 
				(delta < 0 && t.getX() < viewportX))
			{
				scroll = true;
				System.out.println("Scroll");
			}
			else
			{
				scroll = false;
				System.out.println("No Scroll");
			}
		}
		if(scroll)
		{
			//viewportX = Math.max(0, Math.min(menuWidth - viewportWidth, viewportX + delta * scrollStep));
		}
		viewportX = Math.min(0, viewportWidth - (int)(t.getX() + t.getWidth()));
		System.out.println("Selected tab: "+selectedTab);
		this.invalidate();
	}
	private boolean isTabVisible(int tabIndex) {
		Tab t = (Tab)tabs.elementAt(tabIndex);
		return t.getX() < viewportX + viewportWidth &&
			t.getX() + t.getWidth() >= viewportX;
	}
	
	public void paint(Graphics g, int w, int h) {
		g.setClip(0, 0, viewportWidth, tabHeight);
		
		g.setColor(background);
		g.fillRect(0, 0, viewportWidth, tabHeight);
		
		Enumeration e = tabs.elements();
		for (int i=0; e.hasMoreElements(); i++) {
			Tab t = (Tab)e.nextElement();
			g.setColor(i == selectedTab ? t.getBgFocusedColor() : t.getBgColor());
			
			g.fillRoundRect((int)t.getX() + viewportX, menuY, (int)t.getWidth(), tabHeight + cornerRadius, 2 * cornerRadius, 2 * cornerRadius);
			
			g.setColor(i == selectedTab ? t.getForeFocusedColor() : t.getForeColor());
			
			g.drawString(t.getName(), (int)t.getX() + viewportX + cornerRadius + padding, cornerRadius + padding, Graphics.LEFT | Graphics.TOP);
			
			//currentX += t.getWidth() + margin;
		}
	}

	protected int getMinContentHeight() {
		return tabHeight;
	}

	protected int getMinContentWidth() {
		return viewportWidth;
	}

	protected int getPrefContentHeight(int arg0) {
		return tabHeight;
	}

	protected int getPrefContentWidth(int arg0) {
		return viewportWidth;
	}

	public int getSelectedTabIndex() {
		return selectedTab;
	}
}